package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.GetSkuEntity1;
import com.sgb.kjwl.view.ui.activity.self.JoinCompanyActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.ChipActivity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.GoodsSkuChild1;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsSpecActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GoodsSpecAdapter extends BaseRecycleAdapter<GetSkuEntity1> {

    public ItemChildCLickListener itemChildCLickListener;
    private TextView name;
    private LayoutInflater mInflater;
    private Map<Integer, Set<Integer>> map;
    private TagAdapter tagAdapter;

    public void setItemChildCLickListener(ItemChildCLickListener itemChildCLickListener) {
        this.itemChildCLickListener = itemChildCLickListener;
    }

    public GoodsSpecAdapter(Context context, List<GetSkuEntity1> datas) {
        super(context, R.layout.item_goods_spec, datas);
        mInflater = LayoutInflater.from(context);
        map = new HashMap();
    }

    @Override
    public void convert(ItemViewHolder holder, GetSkuEntity1 data) {

        holder.setBinding(BR.viewModel, data);
        name = holder.getItem().findViewById(R.id.name);
        TagFlowLayout tagFlowLayout = holder.getItem().findViewById(R.id.flow_layout);
        if ("1".equals(data.getIsFill())) {
            SpanUtils.with(name).append(data.getParamData()).append(" ").append("*").setForegroundColor(ColorUtils.getColor(R.color.color_main)).create();
        } else {
            name.setText(data.getParamData());
        }
        tagAdapter = new TagAdapter<GoodsSkuChild1>(data.getList()) {
            @Override
            public View getView(FlowLayout parent, int position, GoodsSkuChild1 s) {
                AppCompatTextView tv = (AppCompatTextView) mInflater.inflate(R.layout.item_spec_check,
                        tagFlowLayout, false);
                tv.setText(s.getVal());
                if (s.isOnClick()) {//0 未选中 1选中
                    tv.setBackground(ContextCompat.getDrawable(context, R.drawable.goods_chip_checked_bg));
                    tv.setTextColor(ColorUtils.getColor(R.color.color_EF4033));
                } else {
                    tv.setBackground(ContextCompat.getDrawable(context, R.drawable.goods_chip_unchecked_bg));
                    tv.setTextColor(ColorUtils.getColor(R.color.color_666666));
                }
                return tv;
            }
        };
        tagFlowLayout.setAdapter(tagAdapter);
        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (data.getList().get(position).isOnClick()) {
                    data.getList().get(position).setOnClick(false);
                    notifyDataSetChanged();
                    getSkuCount(datas);
                    if (map.size() != 0) {
                        itemChildCLickListener.checkArr(map, holder.getLayoutPosition());
                    } else {
                        if (!isHaveFill(datas)) {
                            itemChildCLickListener.deleteAll();
                            itemChildCLickListener.addDefaultPriceData();
                        } else {
                            itemChildCLickListener.deleteAll();
                        }
                    }
                } else {
                    data.getList().get(position).setOnClick(true);
                    int allSize = getSkuCount(datas);
                    if (allSize > 100 && allSize <= 1000) {
                        new RoundDialog.Builder(context)
                                .title("温馨提示")
                                .content("勾选后，当前商品规格属性超过100条，将影响商品详情页展示效果，您是否要继续添加？")
                                .cancel("取消")
                                .confirm("继续添加")
                                .confirmCallback(v -> {
                                    itemChildCLickListener.checkArr(map, holder.getLayoutPosition());
                                    notifyDataSetChanged();
                                })
                                .cancelCallback(v -> {
                                    data.getList().get(position).setOnClick(false);
                                    notifyDataSetChanged();
                                })
                                .singleBtn(false)
                                .contentGravity(Gravity.CENTER)
                                .cancelable(false)
                                .show();
                    } else if (allSize > 1000) {
                        data.getList().get(position).setOnClick(false);
                        MToast.showToast(context, "操作失败，商品规格属性数量超过上限");
                    } else {
                        notifyDataSetChanged();
                        if (map.size() != 0) {
                            itemChildCLickListener.checkArr(map, holder.getLayoutPosition());
                        } else {
                            if (!isHaveFill(datas)) {
                                itemChildCLickListener.deleteAll();
                                itemChildCLickListener.addDefaultPriceData();
                            } else {
                                itemChildCLickListener.deleteAll();
                            }
                        }
                    }
                }
                return true;
            }
        });
    }

    private boolean isHaveFill(List<GetSkuEntity1> data) {
        for (int i = 0; i < data.size(); i++) {
            if ("1".equals(data.get(i).getIsFill())) {
                return true;
            }
        }
        return false;
    }

    private boolean isHaveClick(List<GoodsSkuChild1> itemData) {
        for (int i = 0; i < itemData.size(); i++) {
            if (itemData.get(i).isOnClick()) {
                return true;
            }
        }
        return false;
    }


    private int getSkuCount(List<GetSkuEntity1> datas) {
        map.clear();
        for (int i = 0; i < datas.size(); i++) {
            Set<Integer> tempSet = new HashSet<>();
            for (int j = 0; j < datas.get(i).getList().size(); j++) {
                if (datas.get(i).getList().get(j).isOnClick()) {
                    tempSet.add(j);
                    map.put(i, tempSet);
                }
            }
        }
        int allSize = 1;
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            int next = (int) it.next();
            if (map.get(next).size() != 0) {
                allSize = map.get(next).size() * allSize;
            }
        }
        return allSize;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
    }

    public interface ItemChildCLickListener {
        void checkArr(Map<Integer, Set<Integer>> map, int position);

        void addDefaultPriceData();

        void deleteAll();
    }

    public void onItemChildCLickListener(ItemChildCLickListener itemChildCLickListener) {
        this.itemChildCLickListener = itemChildCLickListener;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
